﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using moose.irc.extension;

namespace moose.irc.actions.responses {

    [CLSCompliant(true)]
    [Action(
        Name = "moose-std-rsp-join",
        Description = "This action fires on a join response, it is responsible for generating the proper join event",
        Author = "Mike MacKenzie-Jamer<mikejamer@gmail.com>",
        Version = "0.1",
        Type = ActionTypes.Response)]
    [Trigger(TriggerText = "JOIN")]
    public sealed class JoinResponse : IrcAction {
        public override IrcSessionStates valid_states() {
            return IrcSessionStates.Registered;
        }

        /// <summary>
        /// Joins a channel, or allows another user to join a channel
        /// </summary>
        /// <param name="session">session reference</param>
        /// <param name="message">message passed in</param>
        public override void action(IrcSession session, IrcMessage message) {
            IrcUser source = (IrcUser)session.get_entity(message.Source);
            IrcChannel dest = (IrcChannel)session.get_entity(message.Arguments[0]);
            IrcEvent join_event = null;

            if (source.Nickname == (string)session.Settings_Server[IrcServerSettings.NICKNAME]) {

                //Clear the channel first, because everything should be brand new..
                dest.reset();
                join_event = new IrcEvent(IrcEventTypes.ChannelJoinedSelf,
                    new IrcEventArgument[] {
                        new IrcEventArgument(IrcEventArgumentType.SourceEntity, source),
                        new IrcEventArgument(IrcEventArgumentType.DestinationEntity, dest)
                    });
            }
            else {
                //TODO: Add the user to the channel
                dest.add_user(source);
                join_event = new IrcEvent(IrcEventTypes.ChannelJoined,
                    new IrcEventArgument[] {
                        new IrcEventArgument(IrcEventArgumentType.SourceEntity, source),
                        new IrcEventArgument(IrcEventArgumentType.DestinationEntity, dest)
                    });
            }
            session.notify_event(IrcEvent.createEchoEvent(source.Nickname + " has joined " + dest.Name));
            session.notify_event(join_event);
            return;
        }
    }
}
